package com.ymatou.iapi.liveseller.testcase;

import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.google.gson.JsonObject;
import com.ymatou.iapi.liveseller.parameter.SellerInProgressLiveDetailBean;
import com.ymatou.iapi.liveseller.service.SellerInProgressLiveDetailCall;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.LiveRoomCallService;
import com.ymttest.database.model.LiveRoom;
import com.ymttest.database.sqlwapper.LiveRoomWapper;
import com.ymttest.utils.DesUtil;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.YMTDateUtil;
/**
*买手进行中直播详情
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_SellerInProgressLiveDetail{
	private static SellerInProgressLiveDetailBean sellerinprogresslivedetailBean;
	private static SellerInProgressLiveDetailCall sellerinprogresslivedetailCall;
	private static LiveRoomWapper liveRoomWapper;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("买手进行中直播详情");
	}

	@Before
	public void caseUp() {
		sellerinprogresslivedetailBean=new SellerInProgressLiveDetailBean();
		sellerinprogresslivedetailCall=new SellerInProgressLiveDetailCall();
		liveRoomWapper = new LiveRoomWapper();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(sellerinprogresslivedetailCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	private void verifyDB(JsonObject livedto, int liveid) throws Exception {
		Logger.comment("数据库验证");
		LiveRoom dblive = liveRoomWapper.selectLiveRoomById(liveid);
		Logger.debug(FormatUtil.beanToGSONString(dblive));
		Logger.verifyEquals(livedto.get("GroupId").getAsString(), dblive.getGroupId(), "验证GroupId");
		Logger.verifyEquals(livedto.get("StartTime").getAsString(), dblive.getStartTime().substring(0, 19), "验证StartTime");
		Logger.verifyEquals(livedto.get("Latitude").getAsString(), dblive.getLatitude(), "验证Latitude");
		Logger.verifyEquals(livedto.get("Longitude").getAsString(), dblive.getLongitude(), "验证Longitude");
		Logger.verifyEquals(livedto.get("LiveAddress").getAsString(), dblive.getLiveAddress(), "验证LiveAddress");
		Logger.verifyEquals(livedto.get("LiveCover").getAsString(), dblive.getLiveCover(), "验证LiveCover");
		Logger.verifyEquals(livedto.get("LiveTitle").getAsString(), dblive.getLiveTitle(), "验证LiveTitle");
		Logger.verifyEquals(livedto.get("PreVideoHeight").getAsInt(), dblive.getPreVideoHeight(), "验证PreVideoHeight");
		Logger.verifyEquals(livedto.get("PreVideoLength").getAsInt(), dblive.getPreVideoLength(), "验证PreVideoLength");
		Logger.verifyEquals(livedto.get("PreVideoPic").getAsString(), dblive.getPreVideoPic(), "验证PreVideoPic");
		Logger.verifyEquals(livedto.get("PreVideoWidth").getAsInt(), dblive.getPreVideoWidth(), "验证PreVideoWidth");
		Logger.verifyEquals(livedto.get("SellerName").getAsString(), dblive.getSellerName(), "验证SellerName");
		Logger.verifyEquals(livedto.get("UserId").getAsInt(), dblive.getUserId(), "验证UserId");
		Logger.verifyEquals(livedto.get("PreVideoUrl").getAsString(), dblive.getPreVideoUrl(), "验证PreVideoUrl");
		Logger.verifyEquals(livedto.get("PushUrl").getAsString(), dblive.getPushUrl(), "验证PushUrl");
		String playurl = DesUtil.decrypt(dblive.getPlayUrl(), DesUtil.DES_KEY_STRING, DesUtil.DES_IVPARAMETER);
		// rtmp://12808.liveplay.myqcloud.com/live/12808_SIT1_4028_550
		//这里按照disconf配置走 liveseller现在配置老域名模式
		Logger.debug("playurl：" + playurl);
		String mypurl = "https://" + LiveRoomCallService.PlaySellerDomain + "/live/" + LiveRoomCallService.BizId + "_" + EnvSetup.getEnv().toUpperCase() + "_"
				+ livedto.get("Id") + "_700.flv";
		Logger.verifyEquals(livedto.get("PlayUrl").getAsString(),mypurl, "验证PlayUrl");
		Logger.verifyEquals(livedto.get("HistoryPlayUrl").getAsString(), dblive.getHistoryPlayUrl() == null ? "" : dblive.getHistoryPlayUrl(), "验证HistoryPlayUrl");
		Logger.verifyEquals(livedto.get("CreateTime").getAsString(), dblive.getCreateTime().substring(0, 19), "验证CreateTime");
		Logger.verifyEquals(livedto.get("UpdateTime").getAsString(), dblive.getUpdateTime().substring(0, 19), "验证UpdateTime");
		Logger.verifyEquals(livedto.get("StopReason").getAsString(), dblive.getStopReason(), "验证StopReason");
		Logger.verifyEquals(livedto.get("LivePush").getAsInt(), dblive.getLivePush(), "验证LivePush");

		String LiveStatusText = "";
		int livestatus = dblive.getLiveStatus();
		if (dblive.getLiveStatus() == 0 && dblive.getStartTime().compareTo(YMTDateUtil.getSysTime()) > 0) {
			LiveStatusText = "未开始";
		} else if (dblive.getLiveStatus() == 0 && dblive.getStartTime().compareTo(YMTDateUtil.getSysTime()) <= 0) {
			LiveStatusText = "待开始";
			livestatus = 1;
		} else if (dblive.getLiveStatus() == 2) {
			LiveStatusText = "进行中";
		} else if (dblive.getLiveStatus() == 3) {
			LiveStatusText = "进行中";
		} else if (dblive.getLiveStatus() == 7&&dblive.getLivePush()==0) {
			//未推流8 推流9
			livestatus = 8;
			LiveStatusText = "已关闭";
		} else if (dblive.getLiveStatus() == 7&&dblive.getLivePush()==1) {
			//未推流8 推流9
			livestatus = 9;
			LiveStatusText = "已结束";
		} else if (dblive.getLiveStatus() == 8) {
			LiveStatusText = "已关闭";
		} else if (dblive.getLiveStatus() == 9) {
			LiveStatusText = "已结束";
		} else if (dblive.getLiveStatus() == 10) {
			livestatus = 9;
			LiveStatusText = "已结束";
		}
		Logger.verifyEquals(livedto.get("LiveStatus").getAsInt(), livestatus, "验证LiveStatus");
		Logger.verifyEquals(livedto.get("LiveStatusText").getAsString(), LiveStatusText, "验证LiveStatusText");

		Map rstat = (Map) liveRoomWapper.selectLiveRoomStatById(liveid);
//		Logger.debug(rstat);
		// "ViewNum":185,"OnLineNum":1,"LikeNum":0

	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SellerInProgressLiveDetail_001() {
		Logger.start(true,"买手进行中直播详情成功");
		try {
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(null));
			LiveRoomCallService.SellerLiveOnline(createlive.getId(), createlive.getUserId());
			sellerinprogresslivedetailBean.setSellerId( createlive.getUserId());
			sellerinprogresslivedetailCall.setData(sellerinprogresslivedetailBean);
			sellerinprogresslivedetailCall.callService();			
			Logger.verifyEquals("200", sellerinprogresslivedetailCall.getString("Code"), "验证Code");
			verifyDB(sellerinprogresslivedetailCall.getJsonObject().get("Data").getAsJsonObject().get("LiveRoomDto").getAsJsonObject(), createlive.getId());
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_SellerInProgressLiveDetail_002() {
		Logger.start(true,"不输入sellerid失败");
		try {
			sellerinprogresslivedetailCall.setData(sellerinprogresslivedetailBean);
			sellerinprogresslivedetailCall.callService();			
			Logger.verifyEquals("400", sellerinprogresslivedetailCall.getString("Code"), "验证Code");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_SellerInProgressLiveDetail_003() {
		Logger.start(true,"非进行中直播不展示");
		try {
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(null));
			sellerinprogresslivedetailBean.setSellerId( createlive.getUserId());
			sellerinprogresslivedetailCall.setData(sellerinprogresslivedetailBean);
			sellerinprogresslivedetailCall.callService();			
			Logger.verifyEquals("200", sellerinprogresslivedetailCall.getString("Code"), "验证Code");
			Logger.verifyEquals(true,sellerinprogresslivedetailCall.getJsonObject().get("Data").isJsonNull(), "验证Data");
			LiveRoomCallService.SellerLiveOnline(createlive.getId(), createlive.getUserId());
			LiveRoomCallService.CloseSellerLive(createlive.getId(), createlive.getUserId(), 9);
			sellerinprogresslivedetailCall.callService();			
			Logger.verifyEquals("200", sellerinprogresslivedetailCall.getString("Code"), "验证Code");
			Logger.verifyEquals(true,sellerinprogresslivedetailCall.getJsonObject().get("Data").isJsonNull(), "验证Data");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}